﻿using System;
using System.Collections.Generic;

using System.Text;

namespace RoughJs.Ast
{
    /// <summary>
    /// 2010-01-5 ----- 2010-01-29
    /// 陈曦 1.0
    /// Microsoft Public License (Ms-PL)
    ///This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
    ///1. Definitions
    ///The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
    ///A "contribution" is the original software, or any additions or changes to the software.
    ///A "contributor" is any person that distributes its contribution under this license.
    ///"Licensed patents" are a contributor's patent claims that read directly on its contribution.
    ///2. Grant of Rights
    ///(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
    ///(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
    ///3. Conditions and Limitations
    ///(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
    ///(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
    ///(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
    ///(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
    ///(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. 
    /// </summary>
    public class NativeWith : Scriptable, IdFunctionCall
    {
        private static Object FTAG = "With";

        private static int
            Id_constructor = 1;

        public Scriptable prototype;
        public Scriptable parent;

        private NativeWith()
        {
        }

        public NativeWith(Scriptable parent, Scriptable prototype)
        {
            this.parent = parent;
            this.prototype = prototype;
        }


        public static void init(Scriptable scope, bool sealed1)
        {
            NativeWith obj = new NativeWith();

            obj.setParentScope(scope);
            obj.setPrototype(ScriptableObject.getObjectPrototype(scope));

            IdFunctionObject ctor = new IdFunctionObject(obj, FTAG, Id_constructor,
                                             "With", 0, scope);
            ctor.markAsConstructor(obj);
            if (sealed1)
            {
                ctor.sealObject();
            }
            ctor.exportAsScopeProperty();
        }

        #region IdFunctionCall Members

        public object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args)
        {
            if (f.hasTag(FTAG))
            {
                if (f.methodId() == Id_constructor)
                {
                    throw Context.reportRuntimeError1("msg.cant.call.indirect", "With");
                }
            }
            throw f.unknown();
        }

        #endregion

        #region Scriptable Members

        public string getClassName()
        {
            return "With";
        }

        public object get(string id, Scriptable start)
        {
            if (start == this)
                start = prototype;
            return prototype.get(id, start);
        }

        public object get(int index, Scriptable start)
        {
            if (start == this)
                start = prototype;
            return prototype.get(index, start);
        }

        public bool has(string name, Scriptable start)
        {
            return prototype.has(name, prototype);
        }

        public bool has(int index, Scriptable start)
        {
            return prototype.has(index, prototype);
        }

        public void put(string name, Scriptable start, object value)
        {
            if (start == this)
                start = prototype;
            prototype.put(name, start, value);
        }

        public void put(int index, Scriptable start, object value)
        {
            if (start == this)
                start = prototype;
            prototype.put(index, start, value);
        }

        public void delete(string name)
        {
            prototype.delete(name);
        }

        public void delete(int index)
        {
            prototype.delete(index);
        }

        public Scriptable getPrototype()
        {
            return prototype;
        }

        public void setPrototype(Scriptable prototype)
        {
            this.prototype = prototype;
        }

        public Scriptable getParentScope()
        {
            return parent;
        }

        public void setParentScope(Scriptable parent)
        {
            this.parent = parent;
        }

        public object[] getIds()
        {
            return prototype.getIds();
        }

        public object getDefaultValue(Type hint)
        {
            return prototype.getDefaultValue(hint);
        }

        public bool hasInstance(Scriptable instance)
        {
            return prototype.hasInstance(instance);
        }

        #endregion

        /**
     * Must return null to continue looping or the final collection result.
     */
        public virtual Object updateDotQuery( bool value)
        {
            // NativeWith itself does not support it
            throw new NotSupportedException();
        }

        public static bool isWithFunction(Object functionObj)
        {
            if (functionObj is IdFunctionObject)
            {
                IdFunctionObject f = (IdFunctionObject)functionObj;
                return f.hasTag(FTAG) && f.methodId() == Id_constructor;
            }
            return false;
        }

        public static Object newWithSpecial(Context cx, Scriptable scope, Object[] args)
        {
            ScriptRuntime.checkDeprecated(cx, "With");
            scope = ScriptableObject.getTopLevelScope(scope);
            NativeWith thisObj = new NativeWith();
            thisObj.setPrototype(args.Length == 0
                                 ? ScriptableObject.getClassPrototype(scope,
                                                                      "Object")
                                 : ScriptRuntime.toObject(cx, scope, args[0]));
            thisObj.setParentScope(scope);
            return thisObj;
        }
    }
}
